package com.zxl.live.screen.call.tools;

import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.Display;
import android.view.WindowManager;

import com.zxl.live.screen.call.ui.service.NotifyAccessibilityForJBMR2;
import com.zxl.live.tools.common.ApplicationHelper;
import com.zxl.live.tools.tools.MachineUtils;

public class Utils {

    /**
     * 屏幕是否点亮
     */
    public static boolean isScreenOn(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            return isStateOn(context);
        } else {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            return pm.isScreenOn();
        }
    }

    /**
     * 屏幕是否点亮
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
    public static boolean isStateOn(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return display.getState() == Display.STATE_ON;
    }

    /**
     * 屏幕是否点亮
     * 如果locked为true，表示有两种状态：a、屏幕是黑的 b、目前正处于解锁状态 。 如果locked为false，表示目前未锁屏
     */
    public static boolean isLockScreen() {
        KeyguardManager km = (KeyguardManager) ApplicationHelper.getContext().getSystemService(Context.KEYGUARD_SERVICE);
        try {
            return km.inKeyguardRestrictedInputMode();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isLightFlowAccessibilityEnabled(Context context) {
        String packageName = NotifyAccessibilityForJBMR2.class.getName();
        try {
            int accessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
            if (accessibilityEnabled == 1) {
                String settingValue = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
                if (settingValue != null) {
                    TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
                    mStringColonSplitter.setString(settingValue);
                    while (mStringColonSplitter.hasNext()) {
                        String accessabilityService = mStringColonSplitter.next();
                        if (accessabilityService.contains(packageName)) {
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 是否开启了通知查看权限
     */
    public static boolean isNotifyPermissionEnable() {
        boolean isAccess;
        if (Build.VERSION.SDK_INT >= 18) {
            String listeners = Settings.Secure.getString(ApplicationHelper.getContext().getContentResolver(), "enabled_notification_listeners");
            isAccess = listeners != null && listeners.contains(ApplicationHelper.getContext().getPackageName());
        } else {
            isAccess = Utils.isLightFlowAccessibilityEnabled(ApplicationHelper.getContext());
        }
        return isAccess;
    }

    public static void gotoGrantAccessibility(Context context) {
        try {
            if (MachineUtils.HAS_SDK_JELLY_BEAN_MR2) {
                Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
                context.startActivity(intent);
            } else {
                Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                context.startActivity(intent);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

}
